package com.village.friend.pay.alipay;

import com.alipay.demo.trade.config.Configs;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AliConfigs {

    private static Log log = LogFactory.getLog(AliConfigs.class);
    private static String appCertPath;
    private static String alipayCertPath;
    private static String alipayRootCertPath;
    private static String appName;

    private AliConfigs() {
    }

    public static synchronized void init(String filePath) {
        Configs.init(filePath);
        Configuration configs = Configs.getConfigs();
        if (configs == null) {
            try {
                configs = new PropertiesConfiguration(filePath);
            } catch (ConfigurationException var2) {
                var2.printStackTrace();
            }

            if (configs == null) {
                throw new IllegalStateException("can`t find file by path:" + filePath);
            }
        }


        appCertPath = configs.getString("app_cert_path");
        alipayCertPath = configs.getString("alipay_cert_path");
        alipayRootCertPath = configs.getString("alipay_root_cert_path");
        appName = configs.getString("app_name");
        log.info("配置文件名: " + filePath);
        log.info(description());
    }

    public static String description() {
        StringBuilder sb = new StringBuilder("Configs{ \n");

        sb.append(", 应用公钥证书路径: ").append(appCertPath).append("\n");
        sb.append(", 支付宝公钥证书路径: ").append(alipayCertPath).append("\n");
        sb.append(", 支付宝根公钥证书路径: ").append(alipayRootCertPath).append("\n");
        sb.append(", app名字: ").append(appName).append("\n");
        sb.append("}");
        return sb.toString();
    }

    public static String getAppCertPath() {
        return appCertPath;
    }

    public static String getAlipayCertPath() {
        return alipayCertPath;
    }

    public static String getAlipayRootCertPath() {
        return alipayRootCertPath;
    }

    public static String getAppName() {
        return appName;
    }
}
